package com.another.bidservice.utils.sms;

import cn.hutool.json.JSONException;
import com.another.common.utils.common.MyException;
import com.another.common.utils.result.ResultEnum;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.xml.ws.http.HTTPException;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @ClassName TencentSmsUtil
 * @Description: TODO
 * @Author another
 * @Date 2019-12-18
 * @Version V1.0
 **/
@Component
public class TencentSmsUtil {
    @Value("${appid}")
    private int appid;
    @Value("${appkey}")
    private String appkey;
    @Value("${smsSign}")
    private String smsSign;
    @Value("${smsTemplate1}")
    private String smsTemplate1;

    //发送验证码
    public void sendVerificationCode(String mobile, String verificationCode) {

        String msg = "你的验证码为:" + verificationCode + ",请妥善保管.";
        try {
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.send(0, "86", mobile,
                    msg, "", "");
            System.out.print(result);
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        } catch (com.github.qcloudsms.httpclient.HTTPException e) {
            e.printStackTrace();
        }

    }


    /**
     * 发送动态验证码短信
     *
     * @param phone
     * @param code
     */
    public void send(String phone, String code) {
        try {
            ArrayList<String> listParam = new ArrayList<>();
            listParam.add(code);
//            listParam.add("30");
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
                    506258, listParam, smsSign, "", "");
            System.out.println(result);
        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
            throw new MyException(ResultEnum.ERROR.getCode(), e.getMessage());
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
            throw new MyException(ResultEnum.ERROR.getCode(), e.getMessage());
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
            throw new MyException(ResultEnum.ERROR.getCode(), e.getMessage());
        } catch (com.github.qcloudsms.httpclient.HTTPException e) {
            e.printStackTrace();
            throw new MyException(ResultEnum.ERROR.getCode(), e.getMessage());
        }
    }

    public static void main(String[] args) {
        TencentSmsUtil util = new TencentSmsUtil();
        util.send("13037123710", "8888");
    }

}
